package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealMapper setmealMapper;



    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //向菜品表插入一条数据
        dishMapper.insert(dish);
        Long dishId = dish.getId();

        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insert(flavors);
        }

    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(List<Long> ids) {
        //判断菜品是否为起售

        for (Long l : ids) {
            Integer statusById = dishMapper.getStatusById(l);
            if(statusById.equals(1)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否绑定套餐
        // TODO 未导入套餐表数据，无法判断
        /*for (Long l : ids) {
            BigInteger id = BigInteger.valueOf(l);
            Integer setMeal = dishMapper.getSetmealByDishId(id);
            log.info("套餐id:{}",setMeal);
            if(setMeal.equals(0)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }*/

        //删除菜品
        for (Long l : ids) {
            dishMapper.deleteById(l);
        }

        //删除菜品口味
        for (Long l : ids) {
            dishFlavorMapper.deleteByDishId(l);
        }
    }

    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> flavorByid = dishFlavorMapper.getFlavorByid(id);
        log.info("口味表:{}",flavorByid);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavorByid);
        return dishVO;
    }

    @Override
    public void update(DishVO dishVO) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishVO,dish);
        //更新菜品基本信息
        dishMapper.update(dish);

        //删除原有的口味信息
        dishFlavorMapper.deleteByDishId(dishVO.getId());

        //新增口味
        List<DishFlavor> flavors = dishVO.getFlavors();
        if(flavors != null && !flavors.isEmpty()){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishVO.getId());
            });
            dishFlavorMapper.insert(flavors);
        }




    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {

            List<Dish> dishList = dishMapper.list(dish);

            List<DishVO> dishVOList = new ArrayList<>();

            for (Dish d : dishList) {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(d,dishVO);

                //根据菜品id查询对应的口味
                List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

                dishVO.setFlavors(flavors);
                dishVOList.add(dishVO);
            }

            return dishVOList;
        }

    @Override
    public void updateStatus(Integer status, Integer id) {
        dishMapper.updateStatus(status,id);
    }

}

